package com.foxnicweb.app.patent.controller;

import java.util.*;
import com.foxnicweb.app.domain.patent.*;
import com.foxnicweb.app.domain.patent.meta.PatentProcessRoleMeta;
import com.github.foxnic.commons.lang.StringUtil;
import com.github.foxnic.sql.expr.ConditionExpr;
import org.github.foxnic.web.domain.hrm.meta.EmployeeMeta;
import org.github.foxnic.web.framework.bpm.BpmAssistant;
import org.github.foxnic.web.framework.web.SuperController;
import org.github.foxnic.web.session.SessionUser;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import com.github.foxnic.commons.collection.CollectorUtil;
import com.github.foxnic.dao.entity.ReferCause;
import com.github.foxnic.api.swagger.InDoc;
import org.github.foxnic.web.framework.sentinel.SentinelExceptionUtil;
import com.github.foxnic.api.swagger.ApiParamSupport;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.github.foxnic.web.proxy.bpm.BpmCallbackController;
import org.github.foxnic.web.domain.bpm.BpmActionResult;
import org.github.foxnic.web.domain.bpm.BpmEvent;
import com.foxnicweb.app.proxy.patent.PatentProcessServiceProxy;
import com.foxnicweb.app.domain.patent.meta.PatentProcessVOMeta;
import com.github.foxnic.api.transter.Result;
import com.github.foxnic.dao.data.SaveMode;
import com.github.foxnic.dao.excel.ExcelWriter;
import com.github.foxnic.springboot.web.DownloadUtil;
import com.github.foxnic.dao.data.PagedList;
import java.util.Date;
import java.sql.Timestamp;
import com.github.foxnic.api.error.ErrorDesc;
import com.github.foxnic.commons.io.StreamUtil;
import java.util.Map;
import com.github.foxnic.dao.excel.ValidateResult;
import java.io.InputStream;
import com.foxnicweb.app.domain.patent.meta.PatentProcessMeta;
import java.util.List;
import org.github.foxnic.web.domain.bpm.ProcessInstance;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiImplicitParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.foxnicweb.app.patent.service.IPatentProcessService;
import com.github.foxnic.api.validate.annotations.NotNull;

/**
 * <p>
 * 审批流程实例接口控制器
 * </p>
 * @author 李方捷 , leefangjie@qq.com
 * @since 2023-04-19 16:42:17
 * @version
 */
@InDoc
@Api(tags = "审批流程实例")
@RestController("PatPatentProcessController")
public class PatentProcessController extends SuperController implements BpmCallbackController {

    @Autowired
    private IPatentProcessService patentProcessService;

    /**
     * 添加审批流程实例
     */
    @ApiOperation(value = "添加审批流程实例")
    @ApiImplicitParams({
		@ApiImplicitParam(name = PatentProcessVOMeta.ID, value = "主键", required = true, dataTypeClass = Long.class, example = "2452030"),
		@ApiImplicitParam(name = PatentProcessVOMeta.PROCESS_INSTANCE_ID, value = "流程实例ID", required = false, dataTypeClass = String.class, example = "701103688248197120"),
		@ApiImplicitParam(name = PatentProcessVOMeta.STATUS, value = "审批状态", required = false, dataTypeClass = String.class, example = "A01"),
		@ApiImplicitParam(name = PatentProcessVOMeta.IS_GOT_BASE, value = "是否交底", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.NO_BASE_CONTINUE, value = "继续", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.CAN_OFFICE_DONE, value = "事务所能否完成", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.IS_MATERIAL_COMPLETE, value = "材料是否完整", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.CAN_AGENT_DONE, value = "代理能否完成", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.FINALIZED, value = "是否定稿", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.IS_MATERIAL_COMPLETE, value = "材料是否完整", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.SUBTYPE, value = "案件类型", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.CUSTOMER_NAME, value = "客户名称", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.CUSTOMER_NAME, value = "客户名称", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.CUSTOMER_NAME, value = "客户名称", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.RETRIEVAL, value = "是否检索", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.RETRIEVAL, value = "是否检索", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.RETRIEVAL, value = "是否检索", required = false, dataTypeClass = Integer.class)
	})
    @ApiParamSupport(ignoreDBTreatyProperties = true, ignoreDefaultVoProperties = true, ignorePrimaryKey = true)
    @ApiOperationSupport(order = 1, author = "李方捷 , leefangjie@qq.com")
    @SentinelResource(value = PatentProcessServiceProxy.INSERT, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(PatentProcessServiceProxy.INSERT)
    public Result insert(PatentProcessVO patentProcessVO) {
        Result result = patentProcessService.insert(patentProcessVO, false);
        return result;
    }

    /**
     * 删除审批流程实例
     */
    @ApiOperation(value = "删除审批流程实例")
    @ApiImplicitParams({
		@ApiImplicitParam(name = PatentProcessVOMeta.ID, value = "主键", required = true, dataTypeClass = Long.class, example = "2452030")
	})
    @ApiOperationSupport(order = 2, author = "李方捷 , leefangjie@qq.com")
    @SentinelResource(value = PatentProcessServiceProxy.DELETE, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(PatentProcessServiceProxy.DELETE)
    public Result deleteById(Long id) {
        this.validator().asserts(id).require("缺少id值");
        if (this.validator().failure()) {
            return this.validator().getFirstResult();
        }
        // 引用校验
        ReferCause cause = patentProcessService.hasRefers(id);
        // 判断是否可以删除
        this.validator().asserts(cause.hasRefer()).requireEqual("不允许删除当前记录：" + cause.message(), false);
        if (this.validator().failure()) {
            return this.validator().getFirstResult().messageLevel4Confirm();
        }
        Result result = patentProcessService.deleteByIdLogical(id);
        return result;
    }

    /**
     * 批量删除审批流程实例 <br>
     * 联合主键时，请自行调整实现
     */
    @ApiOperation(value = "批量删除审批流程实例")
    @ApiImplicitParams({
		@ApiImplicitParam(name = PatentProcessVOMeta.IDS, value = "主键清单", required = true, dataTypeClass = List.class, example = "[1,3,4]")
	})
    @ApiOperationSupport(order = 3, author = "李方捷 , leefangjie@qq.com")
    @SentinelResource(value = PatentProcessServiceProxy.DELETE_BY_IDS, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(PatentProcessServiceProxy.DELETE_BY_IDS)
    public Result deleteByIds(List<Long> ids) {
        // 参数校验
        this.validator().asserts(ids).require("缺少ids参数");
        if (this.validator().failure()) {
            return this.validator().getFirstResult();
        }
        // 查询引用
        Map<Long, ReferCause> causeMap = patentProcessService.hasRefers(ids);
        // 收集可以删除的ID值
        List<Long> canDeleteIds = new ArrayList<>();
        for (Map.Entry<Long, ReferCause> e : causeMap.entrySet()) {
            if (!e.getValue().hasRefer()) {
                canDeleteIds.add(e.getKey());
            }
        }
        // 执行删除
        if (canDeleteIds.isEmpty()) {
            // 如果没有一行可以被删除
            return ErrorDesc.failure().message("无法删除您选中的数据行：").data(0).addErrors(CollectorUtil.collectArray(CollectorUtil.filter(causeMap.values(), (e) -> {
                return e.hasRefer();
            }), ReferCause::message, String.class)).messageLevel4Confirm();
        } else if (canDeleteIds.size() == ids.size()) {
            // 如果全部可以删除
            Result result = patentProcessService.deleteByIdsLogical(canDeleteIds);
            return result;
        } else if (canDeleteIds.size() > 0 && canDeleteIds.size() < ids.size()) {
            // 如果部分行可以删除
            Result result = patentProcessService.deleteByIdsLogical(canDeleteIds);
            if (result.failure()) {
                return result;
            } else {
                return ErrorDesc.success().message("已删除 " + canDeleteIds.size() + " 行，但另有 " + (ids.size() - canDeleteIds.size()) + " 行数据无法删除").data(canDeleteIds.size()).addErrors(CollectorUtil.collectArray(CollectorUtil.filter(causeMap.values(), (e) -> {
                    return e.hasRefer();
                }), ReferCause::message, String.class)).messageLevel4Confirm();
            }
        } else {
            // 理论上，这个分支不存在
            return ErrorDesc.success().message("数据删除未处理");
        }
    }

    /**
     * 更新审批流程实例
     */
    @ApiOperation(value = "更新审批流程实例")
    @ApiImplicitParams({
		@ApiImplicitParam(name = PatentProcessVOMeta.ID, value = "主键", required = true, dataTypeClass = Long.class, example = "2452030"),
		@ApiImplicitParam(name = PatentProcessVOMeta.PROCESS_INSTANCE_ID, value = "流程实例ID", required = false, dataTypeClass = String.class, example = "701103688248197120"),
		@ApiImplicitParam(name = PatentProcessVOMeta.STATUS, value = "审批状态", required = false, dataTypeClass = String.class, example = "A01"),
		@ApiImplicitParam(name = PatentProcessVOMeta.IS_GOT_BASE, value = "是否交底", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.NO_BASE_CONTINUE, value = "继续", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.CAN_OFFICE_DONE, value = "事务所能否完成", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.IS_MATERIAL_COMPLETE, value = "材料是否完整", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.CAN_AGENT_DONE, value = "代理能否完成", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.FINALIZED, value = "是否定稿", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.IS_MATERIAL_COMPLETE, value = "材料是否完整", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.SUBTYPE, value = "案件类型", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.CUSTOMER_NAME, value = "客户名称", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.CUSTOMER_NAME, value = "客户名称", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.CUSTOMER_NAME, value = "客户名称", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.RETRIEVAL, value = "是否检索", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.RETRIEVAL, value = "是否检索", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.RETRIEVAL, value = "是否检索", required = false, dataTypeClass = Integer.class)
	})
    @ApiParamSupport(ignoreDBTreatyProperties = true, ignoreDefaultVoProperties = true)
    @ApiOperationSupport(order = 4, author = "李方捷 , leefangjie@qq.com", ignoreParameters = { PatentProcessVOMeta.PAGE_INDEX, PatentProcessVOMeta.PAGE_SIZE, PatentProcessVOMeta.SEARCH_FIELD, PatentProcessVOMeta.FUZZY_FIELD, PatentProcessVOMeta.SEARCH_VALUE, PatentProcessVOMeta.DIRTY_FIELDS, PatentProcessVOMeta.SORT_FIELD, PatentProcessVOMeta.SORT_TYPE, PatentProcessVOMeta.DATA_ORIGIN, PatentProcessVOMeta.QUERY_LOGIC, PatentProcessVOMeta.REQUEST_ACTION, PatentProcessVOMeta.IDS })
    @SentinelResource(value = PatentProcessServiceProxy.UPDATE, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(PatentProcessServiceProxy.UPDATE)
    public Result update(PatentProcessVO patentProcessVO) {
        Result result = patentProcessService.update(patentProcessVO, SaveMode.NOT_NULL_FIELDS, false);
        return result;
    }

    /**
     * 保存审批流程实例
     */
    @ApiOperation(value = "保存审批流程实例")
    @ApiImplicitParams({
		@ApiImplicitParam(name = PatentProcessVOMeta.ID, value = "主键", required = true, dataTypeClass = Long.class, example = "2452030"),
		@ApiImplicitParam(name = PatentProcessVOMeta.PROCESS_INSTANCE_ID, value = "流程实例ID", required = false, dataTypeClass = String.class, example = "701103688248197120"),
		@ApiImplicitParam(name = PatentProcessVOMeta.STATUS, value = "审批状态", required = false, dataTypeClass = String.class, example = "A01"),
		@ApiImplicitParam(name = PatentProcessVOMeta.IS_GOT_BASE, value = "是否交底", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.NO_BASE_CONTINUE, value = "继续", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.CAN_OFFICE_DONE, value = "事务所能否完成", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.IS_MATERIAL_COMPLETE, value = "材料是否完整", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.CAN_AGENT_DONE, value = "代理能否完成", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.FINALIZED, value = "是否定稿", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.IS_MATERIAL_COMPLETE, value = "材料是否完整", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.SUBTYPE, value = "案件类型", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.CUSTOMER_NAME, value = "客户名称", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.CUSTOMER_NAME, value = "客户名称", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.CUSTOMER_NAME, value = "客户名称", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.RETRIEVAL, value = "是否检索", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.RETRIEVAL, value = "是否检索", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.RETRIEVAL, value = "是否检索", required = false, dataTypeClass = Integer.class)
	})
    @ApiParamSupport(ignoreDBTreatyProperties = true, ignoreDefaultVoProperties = true)
    @ApiOperationSupport(order = 5, ignoreParameters = { PatentProcessVOMeta.PAGE_INDEX, PatentProcessVOMeta.PAGE_SIZE, PatentProcessVOMeta.SEARCH_FIELD, PatentProcessVOMeta.FUZZY_FIELD, PatentProcessVOMeta.SEARCH_VALUE, PatentProcessVOMeta.DIRTY_FIELDS, PatentProcessVOMeta.SORT_FIELD, PatentProcessVOMeta.SORT_TYPE, PatentProcessVOMeta.DATA_ORIGIN, PatentProcessVOMeta.QUERY_LOGIC, PatentProcessVOMeta.REQUEST_ACTION, PatentProcessVOMeta.IDS })
    @SentinelResource(value = PatentProcessServiceProxy.SAVE, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(PatentProcessServiceProxy.SAVE)
    public Result save(PatentProcessVO patentProcessVO) {
        Result result = patentProcessService.save(patentProcessVO, SaveMode.DIRTY_OR_NOT_NULL_FIELDS, false);
        return result;
    }

    /**
     * 获取审批流程实例
     */
    @ApiOperation(value = "获取审批流程实例")
    @ApiImplicitParams({
		@ApiImplicitParam(name = PatentProcessVOMeta.ID, value = "主键", required = true, dataTypeClass = Long.class, example = "1")
	})
    @ApiOperationSupport(order = 6, author = "李方捷 , leefangjie@qq.com")
    @SentinelResource(value = PatentProcessServiceProxy.GET_BY_ID, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(PatentProcessServiceProxy.GET_BY_ID)
    public Result<PatentProcess> getById(Long id) {
        Result<PatentProcess> result = new Result<>();
        PatentProcess patentProcess = patentProcessService.getById(id);
        // 填充流程相关的属性
        patentProcessService.joinProcess(patentProcess);
        patentProcessService.dao().fill(patentProcess).with(PatentProcessMeta.PATENT).with(PatentProcessMeta.CASE_AGENT_LIST).with(PatentProcessMeta.BUSINESS_ASSISTANT_LIST).with(PatentProcessMeta.OUT_PERSON_LIST).execute();
        patentProcess.setBusinessAssistantIds(CollectorUtil.collectList(patentProcess.getBusinessAssistantList(), PatentProcessRole::getEmployeeId));
        patentProcess.setCaseAgentIds(CollectorUtil.collectList(patentProcess.getCaseAgentList(), PatentProcessRole::getEmployeeId));
        patentProcess.setOutPersonIds(CollectorUtil.collectList(patentProcess.getOutPersonList(), PatentProcessRole::getEmployeeId));
        result.success(true).data(patentProcess);
        return result;
    }

    /**
     * 批量获取审批流程实例 <br>
     * 联合主键时，请自行调整实现
     */
    @ApiOperation(value = "批量获取审批流程实例")
    @ApiImplicitParams({
		@ApiImplicitParam(name = PatentProcessVOMeta.IDS, value = "主键清单", required = true, dataTypeClass = List.class, example = "[1,3,4]")
	})
    @ApiOperationSupport(order = 3, author = "李方捷 , leefangjie@qq.com")
    @SentinelResource(value = PatentProcessServiceProxy.GET_BY_IDS, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(PatentProcessServiceProxy.GET_BY_IDS)
    public Result<List<PatentProcess>> getByIds(List<Long> ids) {
        Result<List<PatentProcess>> result = new Result<>();
        List<PatentProcess> list = patentProcessService.queryListByIds(ids);
        result.success(true).data(list);
        return result;
    }

    /**
     * 查询审批流程实例
     */
    @ApiOperation(value = "查询审批流程实例")
    @ApiImplicitParams({
		@ApiImplicitParam(name = PatentProcessVOMeta.ID, value = "主键", required = true, dataTypeClass = Long.class, example = "2452030"),
		@ApiImplicitParam(name = PatentProcessVOMeta.PROCESS_INSTANCE_ID, value = "流程实例ID", required = false, dataTypeClass = String.class, example = "701103688248197120"),
		@ApiImplicitParam(name = PatentProcessVOMeta.STATUS, value = "审批状态", required = false, dataTypeClass = String.class, example = "A01"),
		@ApiImplicitParam(name = PatentProcessVOMeta.IS_GOT_BASE, value = "是否交底", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.NO_BASE_CONTINUE, value = "继续", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.CAN_OFFICE_DONE, value = "事务所能否完成", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.IS_MATERIAL_COMPLETE, value = "材料是否完整", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.CAN_AGENT_DONE, value = "代理能否完成", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.FINALIZED, value = "是否定稿", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.IS_MATERIAL_COMPLETE, value = "材料是否完整", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.SUBTYPE, value = "案件类型", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.CUSTOMER_NAME, value = "客户名称", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.CUSTOMER_NAME, value = "客户名称", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.CUSTOMER_NAME, value = "客户名称", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.RETRIEVAL, value = "是否检索", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.RETRIEVAL, value = "是否检索", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.RETRIEVAL, value = "是否检索", required = false, dataTypeClass = Integer.class)
	})
    @ApiOperationSupport(order = 5, author = "李方捷 , leefangjie@qq.com", ignoreParameters = { PatentProcessVOMeta.PAGE_INDEX, PatentProcessVOMeta.PAGE_SIZE })
    @SentinelResource(value = PatentProcessServiceProxy.QUERY_LIST, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(PatentProcessServiceProxy.QUERY_LIST)
    public Result<List<PatentProcess>> queryList(PatentProcessVO sample) {
        Result<List<PatentProcess>> result = new Result<>();
        List<PatentProcess> list = patentProcessService.queryList(sample);
        result.success(true).data(list);
        return result;
    }

    /**
     * 分页查询审批流程实例
     */
    @ApiOperation(value = "分页查询审批流程实例")
    @ApiImplicitParams({
		@ApiImplicitParam(name = PatentProcessVOMeta.ID, value = "主键", required = true, dataTypeClass = Long.class, example = "2452030"),
		@ApiImplicitParam(name = PatentProcessVOMeta.PROCESS_INSTANCE_ID, value = "流程实例ID", required = false, dataTypeClass = String.class, example = "701103688248197120"),
		@ApiImplicitParam(name = PatentProcessVOMeta.STATUS, value = "审批状态", required = false, dataTypeClass = String.class, example = "A01"),
		@ApiImplicitParam(name = PatentProcessVOMeta.IS_GOT_BASE, value = "是否交底", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.NO_BASE_CONTINUE, value = "继续", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.CAN_OFFICE_DONE, value = "事务所能否完成", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.IS_MATERIAL_COMPLETE, value = "材料是否完整", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.CAN_AGENT_DONE, value = "代理能否完成", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.FINALIZED, value = "是否定稿", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.IS_MATERIAL_COMPLETE, value = "材料是否完整", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.SUBTYPE, value = "案件类型", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.CUSTOMER_NAME, value = "客户名称", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.CUSTOMER_NAME, value = "客户名称", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.CUSTOMER_NAME, value = "客户名称", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.RETRIEVAL, value = "是否检索", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.RETRIEVAL, value = "是否检索", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = PatentProcessVOMeta.RETRIEVAL, value = "是否检索", required = false, dataTypeClass = Integer.class)
	})
    @ApiOperationSupport(order = 8, author = "李方捷 , leefangjie@qq.com")
    @SentinelResource(value = PatentProcessServiceProxy.QUERY_PAGED_LIST, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(PatentProcessServiceProxy.QUERY_PAGED_LIST)
    public Result<PagedList<PatentProcess>> queryPagedList(PatentProcessVO sample) {
        SessionUser user = getSessionUser();
        ConditionExpr perm = new ConditionExpr();
        // 如果是经理，可以看到全部
        if (user.permission().checkBusiRoleCode("39820007")) {
            perm.and("1=1");
        } else {
            // 在成员清单里
            // 在成员清单里
            perm.// 或曾经审批过当前单据的
            and(// 或待我审批的(此项目可以按此写法)
            "( " + "exists( select 1 from pat_patent_process_role r where t.id=r.patent_process_id and employee_id=? ) " + "or " + "exists ( select 1 from bpm_task_approval v where t.process_instance_id=v.process_instance_id and v.approval_user_id=?) " + "or " + "exists ( select 1 from bpm_task_assignee v where t.process_instance_id=v.process_instance_id and v.assignee_id=?) " + ")", user.getActivatedEmployeeId(), user.getUserId(), user.getActivatedEmployeeId());
        }
        Result<PagedList<PatentProcess>> result = new Result<>();
        PagedList<PatentProcess> list = patentProcessService.queryPagedList(sample, perm, sample.getPageSize(), sample.getPageIndex());
        // 填充流程相关的属性
        patentProcessService.joinProcess(list);
        patentProcessService.dao().fill(list).with(PatentProcessMeta.PATENT).with(PatentProcessMeta.OUT_PERSON_LIST, PatentProcessRoleMeta.EMPLOYEE, EmployeeMeta.PERSON).with(PatentProcessMeta.CASE_AGENT_LIST, PatentProcessRoleMeta.EMPLOYEE, EmployeeMeta.PERSON).with(PatentProcessMeta.BUSINESS_ASSISTANT_LIST, PatentProcessRoleMeta.EMPLOYEE, EmployeeMeta.PERSON).execute();
        result.success(true).data(list);
        return result;
    }

    /**
     *  流程回调处理
     */
    @SentinelResource(value = PatentProcessServiceProxy.BPM_CALLBACK, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(PatentProcessServiceProxy.BPM_CALLBACK)
    public BpmActionResult onProcessCallback(BpmEvent event) {
        return patentProcessService.onProcessCallback(event);
    }
}
